﻿namespace WMS6.Web
{
    using System;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class AppReg : Page
    {
        protected Button btnReg;
        protected FileUpload fileReg;
        protected HtmlForm form1;
        protected HtmlImage imgLogin;
        protected Label lblMachineCode;
        protected Label lblRegID;
        protected Label lblRegMsg;
        protected TextBox txtMachineCode;

        protected void btnReg_Click(object sender, EventArgs e)
        {
            if (this.fileReg.HasFile)
            {
                try
                {
                    try
                    {
                        string filexml = base.Server.MapPath("") + @"\License\license.qt";
                        if (File.Exists(filexml))
                        {
                            FileInfo fileInfo = new FileInfo(filexml);
                            if (fileInfo.IsReadOnly)
                            {
                                fileInfo.Attributes = FileAttributes.Normal;
                            }
                        }
                        string regContent = string.Empty;
                        using (StreamReader sr = new StreamReader(this.fileReg.FileContent))
                        {
                            regContent = sr.ReadToEnd();
                        }
                        using (StreamWriter sw = new StreamWriter(filexml))
                        {
                            sw.Write(regContent);
                        }
                        string regMsg = this.CheckRegXML();
                        if (!string.IsNullOrEmpty(regMsg))
                        {
                            this.lblRegMsg.Text = regMsg;
                        }
                        else
                        {
                            this.lblRegMsg.Text = "授权文件已生效! 3秒后跳转到系统登录页面!";
                            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>toLogin()</script>", false);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.lblRegMsg.Text = "Error:" + ex.Message;
                    }
                }
                finally
                {
                }
            }
            else
            {
                this.lblRegMsg.Text = "请选择授权文件";
            }
        }

        protected string CheckRegXML()
        {
            string reg = string.Empty;
            string MaxUsers = string.Empty;
            string Warehouses = string.Empty;
            string Expired = string.Empty;
            string ActivationCode = string.Empty;
            string SystemKey = LicenseUtils.GenerateSystemKey();
            EnDeCrypt endeCrypt = new EnDeCrypt();
            XmlDocument doc = new XmlDocument();
            XmlNodeReader reader = null;
            string s = string.Empty;
            string xmlPath = base.Server.MapPath("") + @"\License\license.qt";
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return "授权文件不存在!";
                }
                string xmlString = string.Empty;
                using (StreamReader sr = new StreamReader(xmlPath))
                {
                    xmlString = EnDeCrypt.DecryptString(sr.ReadToEnd());
                }
                doc.LoadXml(xmlString);
                reader = new XmlNodeReader(doc);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            s = reader.Name;
                            goto Label_0194;

                        case XmlNodeType.Text:
                            if (string.Compare(s, "MaxUsers", true) != 0)
                            {
                                break;
                            }
                            MaxUsers = reader.Value;
                            goto Label_0194;

                        default:
                            goto Label_0194;
                    }
                    if (string.Compare(s, "Warehouses", true) == 0)
                    {
                        Warehouses = reader.Value;
                    }
                    else if (string.Compare(s, "Expired", true) == 0)
                    {
                        Expired = reader.Value;
                    }
                    else if (string.Compare(s, "ActivationCode", true) == 0)
                    {
                        ActivationCode = reader.Value;
                    }
                Label_0194:;
                }
                if (!string.Equals(EnDeCrypt.EncryptString(endeCrypt.EnCrypt(SystemKey) + endeCrypt.EnCrypt(MaxUsers) + endeCrypt.EnCrypt(Warehouses) + endeCrypt.EnCrypt(Expired)), ActivationCode))
                {
                    reg = "授权文件无效!";
                }
                return reg;
            }
            catch (Exception ex)
            {
                reg = "Error:" + ex.Message;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return reg;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["regMsg"]))
            {
                this.lblRegMsg.Text = base.Request.QueryString["regMsg"];
            }
            EnDeCrypt endeCrypt = new EnDeCrypt();
            string SystemKey = LicenseUtils.GenerateSystemKey();
            this.txtMachineCode.Text = endeCrypt.EnCrypt(SystemKey);
        }
    }
}

